function LocationTabView(dom, controllBarIcons) {
    this._locationInput = $(".autocomplete_location", dom);
    this._invalidLocationMessageElement = $(".invalid-location-message", dom);

    this._lastUsedLocation = null;
    this._changeMode = null;

    this._controllBarIcons = controllBarIcons;
}

LocationTabView.prototype = {
    render: function(lastUsedLocationCallback) {
        this._lastUsedLocation = lastUsedLocationCallback;

        this._bindAutoCompleteEvents();
        this._bindLocationErrorMessagePopupEvent();
    },

    _bindLocationErrorMessagePopupEvent: function() {
        var me = this;
        this._invalidLocationMessageElement.click(function() {
            me._hideLocationInvalidErrorMessage();
        });
    },

    _bindAutoCompleteEvents: function() {
        var me = this;
        this._locationInput.autoLocation(function() {
            me._delayOnSelctionLocationOperation();
        });

        this._locationInput.lastUsedLocationList(
            function(location) { me._onSelectLocation(location); },
            function() { return me._lastUsedLocation();}
        );

        this._locationInput.blur();
    },

    // hack:: setTimeout to fixissue that cannot get location fullname when mouse select location from autocomplete input.
    _delayOnSelctionLocationOperation: function() {
        var me = this;
        setTimeout(function() {
            var locationFullName = me._locationInput.val();
            var location = pwc.locationCache.getByFullName(locationFullName);
            me._onSelectLocation(location);
        }, 100);
    },

    _onSelectLocation: function(location) {
        this._updateLocationError(location);
        this._changeMode(location);
        this._controllBarIcons.foreach(function(icon) {
            icon.deactivate();
        });
    },

    _updateLocationError: function(location) {
        var isLocationInputEmpty = this._locationInput.hasClass('empty');
        if (!location && !isLocationInputEmpty) {
            this._scheduleShowAndHideLocationInvalidMessage();
            this._locationInput.addClass("error");
        } else {
            this._hideLocationInvalidErrorMessage();
            this._locationInput.removeClass("error");
        }
    },

    _scheduleShowAndHideLocationInvalidMessage: function() {
        var me = this;
        this._invalidLocationMessageElement.removeClass("hidden");
        setTimeout(function() {
            me._hideLocationInvalidErrorMessage();
        }, 3000);
    },

    _hideLocationInvalidErrorMessage: function() {
        this._invalidLocationMessageElement.addClass("hidden");
    },

    setChangeModeCallback: function(callback) {
        this._changeMode = callback;
    }
};
